# import numpy as np
# def correl2d(img, window):
#     m = window.shape[0]
#     n = window.shape[1]
#     #边界通过0灰度值来填充扩展
#     img1 = np.zeros((img.shape[0] + m - 1, img.shape[1] + n - 1))
#     img1[(m - 1) // 2 : (img.shape[0] + (m - 1) // 2) , (n - 1) // 2: (img.shape[1] + (n - 1) // 2)] = img
#     img2 = np.zeros(img.shape)
#     for i in range(img2.shape[0]):
#         for j in range(img2.shape[1]):
#             temp = img1[i : i + m, j: j + n]
#             img2[i,j] = np.sum(np.multiply(temp, window))
#     return (img1, img2)

# #window表示模板， img表示原始图像
# window = np.array([[1, 0, 0], [0, 0, 0], [0, 0, 2]])
# img = np.array([[1, 2, 1, 0, 2, 3], [0, 1, 1, 2, 0, 1], 
#                 [3, 0, 2, 1, 2, 2], [0, 1, 1, 0, 0, 1],
#                 [1, 1, 3, 2, 2, 0], [0, 0, 1, 0, 1, 0]])
# #img1表示边界填充后的图像， img2表示空间滤波结果
# img1, img2 = correl2d(img, window)




# import numpy as np
# import matplotlib.pyplot as plt

# def correl2d(img, window):
#     m = window.shape[0]
#     n = window.shape[1]
#     #边界通过0灰度值来填充扩展
#     img1 = np.zeros((img.shape[0] + m - 1, img.shape[1] + n - 1))
#     img1[(m - 1) // 2 : (img.shape[0] + (m - 1) // 2) , (n - 1) // 2: (img.shape[1] + (n - 1) // 2)] = img
#     img2 = np.zeros(img.shape)
#     for i in range(img2.shape[0]):
#         for j in range(img2.shape[1]):
#             temp = img1[i : i + m, j: j + n]
#             img2[i,j] = np.sum(np.multiply(temp, window))
#     return (img1, img2)

# #window表示模板， img表示原始图像
# window = np.array([[1, 0, 0], [0, 0, 0], [0, 0, 2]])
# img = np.array([[1, 2, 1, 0, 2, 3], [0, 1, 1, 2, 0, 1], 
#                 [3, 0, 2, 1, 2, 2], [0, 1, 1, 0, 0, 1],
#                 [1, 1, 3, 2, 2, 0], [0, 0, 1, 0, 1, 0]])

# # 修改 img 的数据以更好地分布在 0-255 之间
# img = (img - np.min(img)) / (np.max(img) - np.min(img)) * 255

# #img1表示边界填充后的图像， img2表示空间滤波结果
# img1, img2 = correl2d(img, window)

# # 显示结果图像
# plt.figure(figsize=(12, 8))

# plt.subplot(1, 3, 1)
# plt.title('Original Image')
# plt.imshow(img, cmap='gray')
# plt.axis('off')

# plt.subplot(1, 3, 2)
# plt.title('Padded Image')
# plt.imshow(img1, cmap='gray')
# plt.axis('off')

# plt.subplot(1, 3, 3)
# plt.title('Filtered Image')
# plt.imshow(img2, cmap='gray')
# plt.axis('off')

# plt.tight_layout()
# plt.show()

# import numpy as np
# import matplotlib.pyplot as plt
# from matplotlib.image import imread
# from skimage import io, color

# def correl2d(img, window):
#     m = window.shape[0]
#     n = window.shape[1]
#     #边界通过0灰度值来填充扩展
#     img1 = np.zeros((img.shape[0] + m - 1, img.shape[1] + n - 1))
#     img1[(m - 1) // 2 : (img.shape[0] + (m - 1) // 2) , (n - 1) // 2: (img.shape[1] + (n - 1) // 2)] = img
#     img2 = np.zeros(img.shape)
#     for i in range(img2.shape[0]):
#         for j in range(img2.shape[1]):
#             temp = img1[i : i + m, j: j + n]
#             img2[i,j] = np.sum(np.multiply(temp, window))
#     return (img1, img2)

# #window表示模板， img表示原始图像
# # window = np.array([[1, 0, 0], 
# # [0, 0, 0],
# # [0, 0, 2]])
# window = np.array([[0, 0, 0, 0, 0],
#                 [0, 1, 1, 1, 0],
#                 [0, 1, 2, 1, 0],
#                 [0, 1, 1, 1, 0],
#                 [0, 0, 0, 0, 0]])

# # 读取 lena_gray 图像
# img = io.imread(r'c:\Users\26356\Desktop\python\Lenagray.jpg')  

# # 将 RGB 图像转换为灰度图像
# img = color.rgb2gray(img)

# # 修改 img 的数据以更好地分布在 0-255 之间
# img = (img - np.min(img)) / (np.max(img) - np.min(img)) * 255

# #img1表示边界填充后的图像， img2表示空间滤波结果
# img1, img2 = correl2d(img, window)

# # 显示结果图像


# plt.figure(figsize=(12, 8))

# plt.subplot(1, 3, 1)
# plt.title('Original Image')
# plt.imshow(img, cmap='gray')
# plt.axis('off')

# plt.subplot(1, 3, 2)
# plt.title('Padded Image')
# plt.imshow(img1, cmap='gray')
# plt.axis('off')

# plt.subplot(1, 3, 3)
# plt.title('Filtered Image')
# plt.imshow(img2, cmap='gray')
# plt.axis('off')

# plt.tight_layout()
# plt.show()




import numpy as np
import matplotlib.pyplot as plt
from matplotlib.image import imread
from skimage import io, color

def correl2d(img, window):
    m = window.shape[0]
    n = window.shape[1]
    #边界通过0灰度值来填充扩展
    img1 = np.zeros((img.shape[0] + m - 1, img.shape[1] + n - 1))
    img1[(m - 1) // 2 : (img.shape[0] + (m - 1) // 2) , (n - 1) // 2: (img.shape[1] + (n - 1) // 2)] = img
    img2 = np.zeros(img.shape)
    for i in range(img2.shape[0]):
        for j in range(img2.shape[1]):
            temp = img1[i : i + m, j: j + n]
            img2[i,j] = np.sum(np.multiply(temp, window))
    return (img1, img2)

#window表示模板， img表示原始图像
window = np.array([[1, 0, 0], [0, 0, 0], [0, 0, 2]])

# 读取 lena_gray 图像
img = io.imread(r'c:\Users\26356\Desktop\python\Lenagray.jpg')  

# 将 RGB 图像转换为灰度图像
img = color.rgb2gray(img)

# 修改 img 的数据以更好地分布在 0-255 之间
img = (img - np.min(img)) / (np.max(img) - np.min(img)) * 255

#img1表示边界填充后的图像， img2表示空间滤波结果
img1, img2 = correl2d(img, window)

# 检查 img 是否为二维数组
if img.ndim == 2:
    print("img 是二维数组")
else:
    print("img 不是二维数组")

# 显示结果图像
plt.figure(figsize=(12, 8))

plt.subplot(1, 3, 1)
plt.title('Original Image')
plt.imshow(img, cmap='gray')
plt.axis('off')

plt.subplot(1, 3, 2)
plt.title('Padded Image')
plt.imshow(img1, cmap='gray')
plt.axis('off')

plt.subplot(1, 3, 3)
plt.title('Filtered Image')
plt.imshow(img2, cmap='gray')
plt.axis('off')

plt.tight_layout()
plt.show()
